package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.project.iview.IAddEngineeringSurveyOrderView;
import com.cloudsigns.factorymanager2.project.presenter.AddEngineeringSurveyOrderPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.Calendar;
import java.util.Locale;


/**
 * 添加工程勘察单
 * Created by tony on 2018/8/5.
 */

public class AddEngineeringSurveyOrderActivity extends Activity implements OnGetGeoCoderResultListener, View.OnClickListener, IAddEngineeringSurveyOrderView {
    private View rl_back;
    private TextView tv_title;
    private TextView edt__project_name, edt__engineering_name,/* edt_engineering_city,*/ edt__engineering_address, edt_contactse, edt_contactsPhone;
    private EditText edt_completion_date;
    private ImageView img_completion_date;
    private Button btn_save;
    private GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
    private double lat, lon;
    private AddEngineeringSurveyOrderPresenter addEngineeringSurveyOrderPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_engineering_survey_order);

        addEngineeringSurveyOrderPresenter = new AddEngineeringSurveyOrderPresenter(this);
        init();

        // 初始化搜索模块，注册事件监听
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(this);

    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        edt__project_name = findViewById(R.id.edt__project_name);
        edt__engineering_name = findViewById(R.id.edt__engineering_name);
       // edt_engineering_city = findViewById(R.id.edt_engineering_city);
        edt__engineering_address = findViewById(R.id.edt__engineering_address);
        edt_contactse = findViewById(R.id.edt_contactse);
        edt_contactsPhone = findViewById(R.id.edt_contactsPhone);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        img_completion_date=findViewById(R.id.img_completion_date);
        btn_save = findViewById(R.id.btn_save);

        tv_title.setText("现场勘查");

        rl_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
    }

    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();

    }


    private void save() {
        String address=edt__engineering_address.getText().toString().trim();
        mSearch.geocode(new GeoCodeOption()

                //.city(edt_engineering_city.getText().toString().trim())
                .city(address)
                .address(address));

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
                // 完成日期
            case R.id.img_completion_date:
                showDateDialog();
                break;
            case R.id.btn_save:
                save();
                break;
        }
    }




    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
        if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(this, "请输入正确工程地址！", Toast.LENGTH_SHORT).show();
            return;
        }
        lat = geoCodeResult.getLocation().latitude;
        lon = geoCodeResult.getLocation().longitude;

        addEngineeringSurveyOrderPresenter.addEngineeringSurveyOrder();

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {

    }


    @Override
    public long getCompleteDate() {
        long mills = DateUtil.getMillis(edt_completion_date.getText().toString().trim());
//        String date = DateUtil.getYyyy_Mm_Dd(mills);
        return mills;

    }

    @Override
    public String getContacts() {
        return edt_contactse.getText().toString().trim();
    }

    @Override
    public String getContactsPhone() {
        return edt_contactsPhone.getText().toString().trim();
    }

    @Override
    public String getEngineeringAddress() {
        return edt__engineering_address.getText().toString().trim();
    }

    @Override
    public String getEngineeringName() {
        return edt__engineering_name.getText().toString().trim();
    }

    @Override
    public double getLat() {
        return lat;
    }

    @Override
    public double getLon() {
        return lon;
    }

    @Override
    public String getProjectName() {
        return edt__project_name.getText().toString().trim();
    }

    @Override
    public void addCompletionOrderCommentSuccessed(String id) {
        Toast.makeText(this, "保存成功" + id, Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, true);
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void addCompletionOrderCommentFailed(String desc) {
        Toast.makeText(this, "保存失败" + desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
